//
//  ClubChangeSquareNameViewController.swift
//  77Club
//
//  Created by Apple on 2024/12/26.
//

import UIKit

class ClubChangeSquareNameViewController: ClubBaseViewController, UITextFieldDelegate {
    
    private let backButton = c_make_ex(UIButton.self)
        .image("Common_blackBack").end()
    private let cleanButton = c_make_ex(UIButton.self)
        .image("Square_setting_clear").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .text("论坛名称").color(.black).font(16.font_semiBold)
        .alignment(.center).end()
    private let saveButton = c_make_ex(UIButton.self)
        .image("Square_setting_save")
        .image("Square_setting_disSave", state: .disabled).end()
    private let nameTextField = c_make_ex(UITextField.self)
        .placeholder("填写论坛名称").color("#323233")
        .font(14.font_medium).bgColor(.white).end()
    private let cleanView = c_make_ex(UIView.self).bgColor(.white).end()
    private let numberLabel = c_make_ex(UILabel.self)
        .text("100字").color("#969799").font(12.font_medium)
        .alignment(.right).end()
    
    var communityId = ""
    var nameStr = "" {
        didSet{
            nameTextField.text = nameStr
            updateSaveButtonState()
        }
    }
    
    var updateSquareName:((_:String)->())?
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            backButton
            titleLabel
            saveButton
            nameTextField
            cleanView.subviews{
                cleanButton
            }
            numberLabel
        }
        
        backButton.leading(0).top(.statusHeight).width(56).height(44)
        titleLabel.centerHorizontally()
        titleLabel.CenterY == backButton.CenterY
        saveButton.trailing(0).top(.statusHeight).width(77).height(44)
        cleanView.trailing(0).top(.navi_height + 12).width(44.0.Scale).height(52)
        cleanButton.size(20.0.Scale).centerVertically().centerHorizontally()
        nameTextField.leading(0).trailing(44.0.Scale).top(.navi_height + 12).height(52)
        numberLabel.trailing(12.0.Scale)
        numberLabel.Top == nameTextField.Bottom + 8.0.Scale
        
        nameTextField.style { v in
            let leftV = UIView(frame: CGRect(x: 0, y: 0, width: 12.0.Scale, height: 44))
            v.leftView = leftV
            v.leftViewMode = .always
            v.delegate = self
        }
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
        saveButton.addTarget(self, action: #selector(clickSaveButton), for: .touchUpInside)
        cleanButton.addTarget(self, action: #selector(clickCleanButton), for: .touchUpInside)
        
        NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChange(_:)), name: UITextField.textDidChangeNotification, object: nameTextField)
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let currentText = textField.text ?? ""
        let newText = (currentText as NSString).replacingCharacters(in: range, with: string)
        return newText.count <= 100
    }
    
    @objc private func clickSaveButton(){
        
        guard let name = nameTextField.text, name.count > 0 else {
            "论坛名称不能为空~".toast_error
            return
        }
        Task{
            ToastUtil.show()
            let models = await network.requestSpecial(.editSquareInfo(gid: communityId, field: "name", value: name))
            ToastUtil.dismiss()
            if models.isSuccess {        
                "修改成功~".toast_success
                self.updateSquareName?(name)
                self.navigationController?.popViewController(animated: true)
            }else{
                models.errorMessage?.toast_error
            }
        }
    }
    
    @objc private func clickCleanButton(){
        
        nameTextField.text = ""
        updateSaveButtonState()
    }
    
    @objc func textFieldDidChange(_ notification: Notification) {
        
        self.updateSaveButtonState()
    }
    
    private func updateSaveButtonState(){
        if let text = nameTextField.text, text.count > 0 {
            saveButton.isEnabled = true
            cleanButton.isHidden = false
            numberLabel.text = "\(text.count)/100字"
        }else{
            saveButton.isEnabled = false
            cleanButton.isHidden = true
            numberLabel.text = "100字"
        }
    }
}
